﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using micsun.Logic;
using micsun;

namespace commfuncs.SYS
{
   
    public class User :C_DBV
    {
        public User()
        {
        }

        # region 数据库对应字段： -----------------------------------------------
        private System.String m_USERID;
        private System.String m_USERNAME;
        private System.String m_PASSWORD;
        private System.String m_CHGPWDFLAG;
        private System.Double m_VALIDDAY;
        private System.Double m_WARNINGDAY;
        private System.String m_EMAIL;
        private System.String m_TEL;
        private System.String m_EXTNO;
        private System.String m_MOBILPHONE;
        private System.String m_ADDRESS;
        private System.String m_JOBPOSITION;
        private System.String m_DEPTID;
        private System.String m_REMARK;
        private System.DateTime m_PWDEXPIRYDATE;
        private System.DateTime m_CHGPWDDATE;
        private System.String m_LASTMODUSER;
        private System.DateTime m_LASTMODDATE;
        private System.String m_APPUSER;
        private System.DateTime m_APPDATE;
        private System.String m_LOCKUSER;
        private System.Char m_APPFLAG;
        private System.Char m_ISLOCK;
        private System.DateTime m_WARNINGDATE;
        private System.String m_GRPID;
        private System.String m_GRPNAME;
        # endregion//数据库对应字段结束 -----------------------------------------------

        # region  普通属性集 -----------------------------------------------
        public System.String USERID
        {
            get { return m_USERID; }
            set { m_USERID = value; }
        }
        public System.String USERNAME
        {
            get { return m_USERNAME; }
            set { m_USERNAME = value; }
        }
        public System.String PASSWORD
        {
            get { return m_PASSWORD; }
            set { m_PASSWORD = value; }
        }
        public System.String CHGPWDFLAG
        {
            get { return m_CHGPWDFLAG; }
            set { m_CHGPWDFLAG = value; }
        }
        public System.Double VALIDDAY
        {
            get { return m_VALIDDAY; }
            set { m_VALIDDAY = value; }
        }
        public System.Double WARNINGDAY
        {
            get { return m_WARNINGDAY; }
            set { m_WARNINGDAY = value; }
        }
        public System.String EMAIL
        {
            get { return m_EMAIL; }
            set { m_EMAIL = value; }
        }
        public System.String TEL
        {
            get { return m_TEL; }
            set { m_TEL = value; }
        }
        public System.String EXTNO
        {
            get { return m_EXTNO; }
            set { m_EXTNO = value; }
        }
        public System.String MOBILPHONE
        {
            get { return m_MOBILPHONE; }
            set { m_MOBILPHONE = value; }
        }
        public System.String ADDRESS
        {
            get { return m_ADDRESS; }
            set { m_ADDRESS = value; }
        }
        public System.String JOBPOSITION
        {
            get { return m_JOBPOSITION; }
            set { m_JOBPOSITION = value; }
        }
        public System.String DEPTID
        {
            get { return m_DEPTID; }
            set { m_DEPTID = value; }
        }
        public System.String REMARK
        {
            get { return m_REMARK; }
            set { m_REMARK = value; }
        }
        public System.String LASTMODUSER
        {
            get { return m_LASTMODUSER; }
            set { m_LASTMODUSER = value; }
        }
        public System.DateTime LASTMODDATE
        {
            get { return m_LASTMODDATE; }
            set { m_LASTMODDATE = value; }
        }
        public System.String APPUSER
        {
            get { return m_APPUSER; }
            set { m_APPUSER = value; }
        }
        public System.DateTime APPDATE
        {
            get { return m_APPDATE; }
            set { m_APPDATE = value; }
        }
        public System.String LOCKUSER
        {
            get { return m_LOCKUSER; }
            set { m_LOCKUSER = value; }
        }
        public System.Char APPFLAG
        {
            get { return m_APPFLAG; }
            set { m_APPFLAG = value; }
        }
        public System.Char ISLOCK
        {
            get { return m_ISLOCK; }
            set { m_ISLOCK = value; }
        }
        public System.DateTime WARNINGDATE
        {
            get { return m_WARNINGDATE; }
            set { m_WARNINGDATE = value; }
        }
        public System.String GRPID
        {
            get { return m_GRPID; }
            set { m_GRPID = value; }
        }
        public System.String GRPNAME
        {
            get { return m_GRPNAME; }
            set { m_GRPNAME = value; }
        }
        # endregion 普通属性集结束-----------------------------------------------

        public bool Select(string KeyID) // 查询用户组信息表
        {
            bool Found = false;
            string selectCmd = "select ISMGRP.GRPID,ISMGRP.GRPDESC,ISMBUR.* from ISMBTG,ISMGRP,ISMBUR"
                + " where ISMBUR.USERID=ISMBTG.USERID and ISMBTG.GRPID=ISMGRP.GRPID and ISMBUR.USERID='" + KeyID + "'";
            try
            {
                IDataReader dr = micsunDB.CreateDataAccessor().ExecuteReader(selectCmd, null);
                if (dr.Read())
                {
                    Found = true;
                    ReadDataReader(dr);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error in C用户组信息表:Select()-> " + e.ToString());
            }
            finally
            {
            }
            return Found;
        }

        private void ReadDataReader(IDataReader m_SqlDataReader)
        {
            m_USERID = m_SqlDataReader["USERID"].ToString();
            m_USERNAME = m_SqlDataReader["USERNAME"].ToString();
            m_PASSWORD = m_SqlDataReader["PASSWORD"].ToString();
            if (m_SqlDataReader["CHGPWDFLAG"] != DBNull.Value)
            {
                m_CHGPWDFLAG = m_SqlDataReader["CHGPWDFLAG"].ToString();
            }
            if (m_SqlDataReader["VALIDDAY"] != DBNull.Value)
            {
                m_VALIDDAY = System.Double.Parse(m_SqlDataReader["VALIDDAY"].ToString());
            }
            if (m_SqlDataReader["WARNINGDAY"] != DBNull.Value)
            {
                m_WARNINGDAY = System.Double.Parse(m_SqlDataReader["WARNINGDAY"].ToString());
            }
            m_EMAIL = m_SqlDataReader["EMAIL"].ToString();
            m_TEL = m_SqlDataReader["TEL"].ToString();
            m_EXTNO = m_SqlDataReader["EXTNO"].ToString();
            m_MOBILPHONE = m_SqlDataReader["MOBILPHONE"].ToString();
            m_ADDRESS = m_SqlDataReader["ADDRESS"].ToString();
            m_JOBPOSITION = m_SqlDataReader["JOBPOSITION"].ToString();
            m_DEPTID = m_SqlDataReader["DEPTID"].ToString();
            m_REMARK = m_SqlDataReader["REMARK"].ToString();
            if (m_SqlDataReader["PWDEXPIRYDATE"] != DBNull.Value)
            {
                m_PWDEXPIRYDATE = System.DateTime.Parse(m_SqlDataReader["PWDEXPIRYDATE"].ToString());
            }
            if (m_SqlDataReader["CHGPWDDATE"] != DBNull.Value)
            {
                m_CHGPWDDATE = System.DateTime.Parse(m_SqlDataReader["CHGPWDDATE"].ToString());
            }
            m_LASTMODUSER = m_SqlDataReader["LASTMODUSER"].ToString();
            if (m_SqlDataReader["LASTMODDATE"] != DBNull.Value)
            {
                m_LASTMODDATE = System.DateTime.Parse(m_SqlDataReader["LASTMODDATE"].ToString());
            }
            m_APPUSER = m_SqlDataReader["APPUSER"].ToString();
            if (m_SqlDataReader["APPDATE"] != DBNull.Value)
            {
                m_APPDATE = System.DateTime.Parse(m_SqlDataReader["APPDATE"].ToString());
            }
            m_LOCKUSER = m_SqlDataReader["LOCKUSER"].ToString();
            if (m_SqlDataReader["APPFLAG"] != DBNull.Value)
            {
                m_APPFLAG = System.Char.Parse(m_SqlDataReader["APPFLAG"].ToString());
            }
            if (m_SqlDataReader["ISLOCK"] != DBNull.Value)
            {
                m_ISLOCK = System.Char.Parse(m_SqlDataReader["ISLOCK"].ToString());
            }
            if (m_SqlDataReader["WARNINGDATE"] != DBNull.Value)
            {
                m_WARNINGDATE = System.DateTime.Parse(m_SqlDataReader["WARNINGDATE"].ToString());
            }
            m_GRPID = m_SqlDataReader["GRPID"].ToString();
            m_GRPNAME = m_SqlDataReader["GRPDESC"].ToString();
        }

        public bool Add()//新增用户
        {
            bool res = true;
            LogicDB ldb = new LogicDB();
            IDbTransaction tran = CreateTran(out ldb);
            try
            {
                PASSWORD = CommonFunction.BytesToString(micsun.CryptoServices.ComputeHash(PASSWORD));
                string sql = "INSERT INTO ISMBUR (USERID,USERNAME,PASSWORD, LASTMODUSER, LASTMODDATE, ISLOCK, APPFLAG)"
                + " VALUES ('" + USERID + "','" + USERNAME + "','" + PASSWORD + "','" + LASTMODUSER + "','" + LASTMODDATE + "','0','0')";
                RunTran(ldb, sql);

                sql = "INSERT INTO ISMBTG (USERID,GRPID,LASTMODUSER,LASTMODDATE,ISLOCK,APPFLAG)"
                + " VALUES ('" + USERID + "','" + GRPID + "','" + LASTMODUSER + "','" + LASTMODDATE + "','0','0')";
                RunTran(ldb, sql);
                tran.Commit();
            }
            catch (Exception ex)
            {
                res = false;
                tran.Rollback();
            }
            finally
            {
                ldb.Connection.Close();
            }
            return res;
        }

        public bool Update(bool IsChange)//修改用户
        {
            bool res = true;
            LogicDB ldb = new LogicDB();
            IDbTransaction tran = CreateTran(out ldb);
            try
            {
                if (IsChange)
                {
                    PASSWORD = CommonFunction.BytesToString(micsun.CryptoServices.ComputeHash(PASSWORD));
                }
                string sql = "UPDATE ISMBUR SET USERNAME='" + USERNAME + "',PASSWORD='" + PASSWORD + "',LASTMODUSER='" + LASTMODUSER
                    + "',LASTMODDATE='" + LASTMODDATE + "' WHERE USERID='" + USERID + "'";
                RunTran(ldb, sql);

                sql = "UPDATE ISMBTG SET GRPID='" + GRPID + "',LASTMODUSER='" + LASTMODUSER + "',LASTMODDATE='" + LASTMODDATE
                    + "' WHERE USERID='" + USERID + "'";
                RunTran(ldb, sql);
                tran.Commit();
            }
            catch
            {
                res = false;
                tran.Rollback();
            }
            finally
            {
                ldb.Connection.Close();
            }
            return res;
        }

        public bool Delete()
        {
            bool res = true;
            LogicDB ldb = new LogicDB();
            IDbTransaction tran = CreateTran(out ldb);
            try
            {
                string sql = "DELETE from ISMBUR Where USERID='" + USERID + "'";
                RunTran(ldb, sql);
                sql = "DELETE from ISMBTG Where USERID='" + USERID + "'";
                RunTran(ldb, sql);
                tran.Commit();
            }
            catch
            {
                res = false;
                tran.Rollback();
            }
            finally
            {
                ldb.Connection.Close();
            }
            return res;
        }

        public bool IsHaveSameID(string _id)
        {
            string sql = "SELECT * FROM ISMBUR  Where USERID='" + _id + "'";
            DataTable dt = GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
